﻿Imports BLL
Public Class FrmIngresarPago
    Public gestorPagos As New GestorPagos()
    Public gestorMantenimientoUsuarios As New GestorMantenimientoUsuarios()
    Public gestorParametrosSistema As New GestorParametrosSistema()
    Private validador As ValidadorDeFormularios

    Private Sub bntSalir_Click(sender As Object, e As EventArgs)
        Me.Close()
    End Sub

    Private Sub btnSalir_Click(sender As Object, e As EventArgs) Handles btnSalir.Click
        Me.Close()
    End Sub

    Private Sub FrmIngresarPago_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cmbReqCliente.DisplayMember = "Identificacion"
        cmbReqCliente.ValueMember = "IdUsuario"
        cmbReqCliente.DataSource = gestorPagos.GetListaUsuariosPagar().Tables(0)
        validador = New ValidadorDeFormularios(Me, ErrorProvider1)
        Me.loadPagos()
    End Sub

    Private Sub loadPagos()
        With dgvPagosUsuarios
            .AutoGenerateColumns = False
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .Columns.Clear()

            .Columns.Add("IdPago", "Id")
            .Columns.Add("IdCliente", "Identificación usuario")
            .Columns.Add("Tipo", "Tipo de pago")
            .Columns.Add("FechaDePago", "Fecha de pago")
            .Columns.Add("MontoPagado", "Monto cancelado")
            .Columns.Add("DiasEfectivos", "Dias efectivos")


            .Columns(0).DataPropertyName = "IdPago"
            .Columns(0).Visible = False
            .Columns(0).Width = 100
            .Columns(1).DataPropertyName = "IdCliente"
            .Columns(1).Visible = True
            .Columns(1).Width = 200
            .Columns(2).DataPropertyName = "Tipo"
            .Columns(2).Visible = True
            .Columns(2).Width = 200
            .Columns(3).DataPropertyName = "FechaDePago"
            .Columns(3).Visible = True
            .Columns(3).Width = 200
            .Columns(4).DataPropertyName = "MontoPagado"
            .Columns(4).Visible = True
            .Columns(4).Width = 200
            .Columns(5).DataPropertyName = "DiasEfectivos"
            .Columns(5).Visible = True
            .Columns(5).Width = 200
            .DataSource = New BindingSource(gestorPagos.listarPagos(), Nothing)
        End With
    End Sub

    Private Sub cmbReqCliente_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbReqCliente.SelectedIndexChanged
        Dim selectedItem As Integer = DirectCast(cmbReqCliente.SelectedItem, DataRowView).Item("IdUsuario")
        Dim usuario As EntitiesLayer.Usuario = gestorMantenimientoUsuarios.consultarPorId(selectedItem)
        Me.txtNombre.Text = usuario.Nombre + " " + usuario.PApellido + " " + usuario.SApellido

    End Sub

    Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles radioBtnMatricula.CheckedChanged
        Dim parametrosSistema As EntitiesLayer.ParametrosSistema = gestorParametrosSistema.ObtenerParametrosSistema()
        Me.txtStrMonto.Text = parametrosSistema.CostoMatricula
    End Sub

    Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles radioBtnMembresia.CheckedChanged
        Dim parametrosSistema As EntitiesLayer.ParametrosSistema = gestorParametrosSistema.ObtenerParametrosSistema()
        Me.txtStrMonto.Text = parametrosSistema.CostoMembresia
    End Sub

    Private Sub btnIncluir_Click(sender As Object, e As EventArgs) Handles btnIncluir.Click
        If validador.ValidarFormulario() Then

            Dim idCliente As Integer = DirectCast(cmbReqCliente.SelectedItem, DataRowView).Item("idUsuario")
            Dim montoPagado As Decimal = Convert.ToDecimal(Me.txtStrMonto.Text)
            Dim tipo As EntitiesLayer.TipoPago
            If radioBtnMatricula.Checked = True Then
                tipo = EntitiesLayer.TipoPago.Matricula
            Else
                tipo = EntitiesLayer.TipoPago.Membresia
            End If
            Dim parametrosSistema As EntitiesLayer.ParametrosSistema = gestorParametrosSistema.ObtenerParametrosSistema()
            Dim diasEfectivos As Integer = Convert.ToInt32(parametrosSistema.FrecuenciaCobroMembresia)

            If gestorPagos.insertarPago(idCliente, montoPagado, tipo, diasEfectivos) Then
                MsgBox("El pago fue procesado correctamente.", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, Me.Text)
                Me.txtStrMonto.Text = ""
                Me.loadPagos()
            Else
                MsgBox("El pago no fue registrado!!", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, Me.Text)
            End If
        End If
        Me.loadPagos()
    End Sub
End Class